package com.yonghui.summer.code.dao.utils;

import com.yonghui.summer.po.ColumnValue;
import com.yonghui.summer.po.MybatisParam;
import com.yonghui.summer.po.TableMapping;

import java.util.List;

/**
 * @auther yangchuan
 * @date 2021-03-27 14:39
 */
public class ParamValueBuilder {


    private MybatisParam mybatisParam;


    private TableMapping tableMapping;


    public static ParamValueBuilder builder() {
        return new ParamValueBuilder();
    }

    public ParamValueBuilder setMybatisParam(MybatisParam mybatisParam) {
        this.mybatisParam = mybatisParam;
        return this;
    }

    public ParamValueBuilder setTableMapping(TableMapping tableMapping) {
        this.tableMapping = tableMapping;
        return this;
    }

    public String build() {
        StringBuilder sb = new StringBuilder();
        if (tableMapping == null) {
            sb.append("pvs.add(new com.yonghui.summer.po.ParamValue(")
                    .append("\"")
                    .append(mybatisParam.getName())
                    .append("\"")
                    .append(",")
                    .append(mybatisParam.getArgs()).append("));\n");
        } else {
            List<ColumnValue> cvs = tableMapping.getColumnValues();
            for (ColumnValue cv : cvs) {
                sb.append("pvs.add(new com.yonghui.summer.po.ParamValue(")
                        .append("\"")
                        .append(mybatisParam.getName())
                        .append(".")
                        .append(cv.getFieldName())
                        .append("\"")
                        .append(",")
                        .append(mybatisParam.getArgs()).append(".").append(cv.getReadMethodName()).append("()));\n");
            }
        }

        return sb.toString();
    }

}
